﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super_Wall_Breaker.Sources.TeamElements.Players;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Super_Wall_Breaker.Sources.GameEngine;

namespace Super_Wall_Breaker.Sources.TeamElements
{
    /// <summary>
    /// This class represents a Team of players.
    /// </summary>
    public class Team
    {
        private TeamIndex _idTeam;
        private int _score; 
        private int _brickScore; 
        private List<Player> _players;

        /// <summary>
        /// Gets or sets the id team, ie the team position.
        /// </summary>
        /// <value>
        /// The position of the team.
        /// </value>
        public TeamIndex IdTeam
        {
            get { return _idTeam; }
            set { _idTeam = value; }
        }

        /// <summary>
        /// Gets or sets the score.
        /// </summary>
        /// <value>
        /// The total score of the team.
        /// </value>
        public int Score
        {
            get { return _score; }
            set { _score = value; }
        }

        /// <summary>
        /// Gets or sets the brick score.
        /// </summary>
        /// <value>
        /// The brick score of the team, ie the score gets by destroying bricks.
        /// </value>
        public int BrickScore
        {
            get { return _brickScore; }
            set { _brickScore = value; }
        }

        /// <summary>
        /// Gets or sets the players.
        /// </summary>
        /// <value>
        /// The players member of the team.
        /// </value>
        public List<Player> Players
        {
            get { return _players; }
            set { _players = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Team"/> class.
        /// </summary>
        /// <param name="teamIndex">Index of the team.</param>
        public Team(TeamIndex teamIndex)
        {
            this._players = new List<Player>();
            this._idTeam = teamIndex;
            this._brickScore = 0;
            this._score = 0;
        }

        /// <summary>
        /// Adds the player given in the team.
        /// </summary>
        /// <param name="player">The player to add.</param>
        public void AddPlayer(Player player)
        {
            if (player != null)
                this._players.Add(player);
        }

        /// <summary>
        /// Updates the model in function of the specified game time.
        /// </summary>
        /// <param name="gameTime">A snapshot of the current time.</param>
        public void Update(GameTime gameTime)
        {
            foreach(Player player in _players)
            {
                player.Update(gameTime);
            }
        }

        /// <summary>
        /// This is called when the team should draw itself. It draws each team's bat, the scores and the time left.
        /// </summary>
        /// <param name="spriteBatch">Enables a group of sprites to be drawn using the same settings.</param>
        /// <param name="spriteFont">Represents a font texture. </param>
        /// <param name="timeLeft">The time left, in ms.</param>
        public void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont, int timeLeft) 
        {

            Vector2 position = new Vector2(10, 10);

            //Bats drawing
            foreach (Player p in this.Players)
            {
                p.Bat.Draw(spriteBatch);
            }

            //Score and time Drawing
            if (this.IdTeam == TeamIndex.bottom)
            {
                position = new Vector2(10, GlobalConfiguration.DEFAULT_SCREEN_HEIGHT- 65);
            }
            
            int minuteLeft = timeLeft / 60000;
            int secondLeft = (timeLeft - minuteLeft * 60000) / 1000;
            int hourLeft = minuteLeft / 60;

            string messageScore = String.Format("Score : {0} \n", this._score);
            string messageBrickTime = String.Format("Brick score : {0} \nTime left : {1}:{2}", this._brickScore, minuteLeft, secondLeft);

            if (hourLeft > 0)
            {
                minuteLeft -= hourLeft * 60;
                messageBrickTime = String.Format("Brick score : {0} \nTime left : {1}:{2}:{3}", this._brickScore, hourLeft, minuteLeft, secondLeft);
            }

            spriteBatch.DrawString(spriteFont, messageBrickTime, new Vector2(position.X,position.Y + 20), Color.Beige);
            spriteBatch.DrawString(spriteFont, messageScore, position, Color.Beige, 0, Vector2.Zero, 1.1f, SpriteEffects.None, 0);
        }
    }
}
